package com.java110.mall.shop.api;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.context.Environment;
import com.java110.dto.vipUser.ShopVipUserDto;
import com.java110.po.shop.ShopVipLevelPo;
import com.java110.po.shop.ShopVipUserPo;
import com.java110.mall.shop.bmo.shopVipLevel.IDeleteShopVipLevelBMO;
import com.java110.mall.shop.bmo.shopVipLevel.IGetShopVipLevelBMO;
import com.java110.mall.shop.bmo.shopVipLevel.ISaveShopVipLevelBMO;
import com.java110.mall.shop.bmo.shopVipLevel.IUpdateShopVipLevelBMO;
import com.java110.mall.shop.bmo.shopVipUser.IDeleteShopVipUserBMO;
import com.java110.mall.shop.bmo.shopVipUser.IGetShopVipUserBMO;
import com.java110.mall.shop.bmo.shopVipUser.ISaveShopVipUserBMO;
import com.java110.mall.shop.bmo.shopVipUser.IUpdateShopVipUserBMO;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping(value = "/shopVipUser")
public class ShopVipUserApi {

    @Autowired
    private ISaveShopVipUserBMO saveShopVipUserBMOImpl;
    @Autowired
    private IUpdateShopVipUserBMO updateShopVipUserBMOImpl;
    @Autowired
    private IDeleteShopVipUserBMO deleteShopVipUserBMOImpl;

    @Autowired
    private IGetShopVipUserBMO getShopVipUserBMOImpl;

    @Autowired
    private ISaveShopVipLevelBMO saveShopVipLevelBMOImpl;
    @Autowired
    private IUpdateShopVipLevelBMO updateShopVipLevelBMOImpl;
    @Autowired
    private IDeleteShopVipLevelBMO deleteShopVipLevelBMOImpl;

    @Autowired
    private IGetShopVipLevelBMO getShopVipLevelBMOImpl;

    /**
     * 微信保存消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /shopVipUser/saveShopVipUser
     * @path /app/shopVipUser/saveShopVipUser
     */
    @RequestMapping(value = "/saveShopVipUser", method = RequestMethod.POST)
    public ResponseEntity<String> saveShopVipUser(@RequestHeader(value = "store-id") String storeId,
                                                  @RequestBody JSONObject reqJson) {

        Assert.hasKeyAndValue(reqJson, "shopId", "请求报文中未包含shopId");
        Assert.hasKeyAndValue(reqJson, "vipName", "请求报文中未包含vipName");
        Assert.hasKeyAndValue(reqJson, "vipTel", "请求报文中未包含vipTel");
        Assert.hasKeyAndValue(reqJson, "vipLevel", "请求报文中未包含vipLevel");
        Assert.hasKeyAndValue(reqJson, "startTime", "请求报文中未包含startTime");
        Assert.hasKeyAndValue(reqJson, "endTime", "请求报文中未包含endTime");


        ShopVipUserPo shopVipUserPo = BeanConvertUtil.covertBean(reqJson, ShopVipUserPo.class);
        shopVipUserPo.setStoreId(storeId);
        shopVipUserPo.setShopScope("0");
        shopVipUserPo.setUserId("-1");
        return saveShopVipUserBMOImpl.save(shopVipUserPo);
    }

    /**
     * 微信修改消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /shopVipUser/updateShopVipUser
     * @path /app/shopVipUser/updateShopVipUser
     */
    @RequestMapping(value = "/updateShopVipUser", method = RequestMethod.POST)
    public ResponseEntity<String> updateShopVipUser(@RequestHeader(value = "store-id") String storeId,
                                                    @RequestBody JSONObject reqJson) {

        Environment.isDevEnv();
        Assert.hasKeyAndValue(reqJson, "shopId", "请求报文中未包含shopId");
        Assert.hasKeyAndValue(reqJson, "vipName", "请求报文中未包含vipName");
        Assert.hasKeyAndValue(reqJson, "vipTel", "请求报文中未包含vipTel");
        Assert.hasKeyAndValue(reqJson, "vipLevel", "请求报文中未包含vipLevel");
        Assert.hasKeyAndValue(reqJson, "startTime", "请求报文中未包含startTime");
        Assert.hasKeyAndValue(reqJson, "endTime", "请求报文中未包含endTime");
        Assert.hasKeyAndValue(reqJson, "vipId", "vipId不能为空");


        ShopVipUserPo shopVipUserPo = BeanConvertUtil.covertBean(reqJson, ShopVipUserPo.class);
        shopVipUserPo.setStoreId(storeId);

        return updateShopVipUserBMOImpl.update(shopVipUserPo);
    }

    /**
     * 微信删除消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /shopVipUser/deleteShopVipUser
     * @path /app/shopVipUser/deleteShopVipUser
     */
    @RequestMapping(value = "/deleteShopVipUser", method = RequestMethod.POST)
    public ResponseEntity<String> deleteShopVipUser(@RequestHeader(value = "store-id") String storeId,
                                                    @RequestBody JSONObject reqJson) {
        Environment.isDevEnv();
        Assert.hasKeyAndValue(reqJson, "shopId", "商铺不能为空");

        Assert.hasKeyAndValue(reqJson, "vipId", "vipId不能为空");


        ShopVipUserPo shopVipUserPo = BeanConvertUtil.covertBean(reqJson, ShopVipUserPo.class);
        shopVipUserPo.setStoreId(storeId);
        return deleteShopVipUserBMOImpl.delete(shopVipUserPo);
    }



    /**
     * 微信删除消息模板
     *
     * @param shopId 商铺ID
     * @return
     * @serviceCode /shopVipUser/searchShopVipUser
     * @path /app/shopVipUser/searchShopVipUser
     */
    @RequestMapping(value = "/searchShopVipUser", method = RequestMethod.GET)
    public ResponseEntity<String> searchShopVipUser(@RequestHeader(value = "store-id") String storeId,
                                                    @RequestParam(value = "shopId") String shopId,
                                                    @RequestParam(value = "vipUserSearchValue") String vipUserSearchValue,
                                                    @RequestParam(value = "page") int page,
                                                    @RequestParam(value = "row") int row) {
        ShopVipUserDto shopVipUserDto = new ShopVipUserDto();
        shopVipUserDto.setPage(page);
        shopVipUserDto.setRow(row);
        shopVipUserDto.setShopId(shopId);
        shopVipUserDto.setVipTel(vipUserSearchValue);
        shopVipUserDto.setStoreId(storeId);
        return getShopVipUserBMOImpl.search(shopVipUserDto);
    }

    /**
     * 微信保存消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /shopVipUser/saveShopVipLevel
     * @path /app/shopVipUser/saveShopVipLevel
     */
    @RequestMapping(value = "/saveShopVipLevel", method = RequestMethod.POST)
    public ResponseEntity<String> saveShopVipLevel(@RequestHeader(value = "store-id") String storeId,
                                                   @RequestBody JSONObject reqJson) {

        Assert.hasKeyAndValue(reqJson, "levelName", "请求报文中未包含levelName");
        Assert.hasKeyAndValue(reqJson, "vipLevel", "请求报文中未包含vipLevel");

        ShopVipLevelPo shopVipLevelPo = BeanConvertUtil.covertBean(reqJson, ShopVipLevelPo.class);
        shopVipLevelPo.setStoreId(storeId);

        return saveShopVipLevelBMOImpl.save(shopVipLevelPo,reqJson);
    }

    /**
     * 微信修改消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /shopVipUser/updateShopVipLevel
     * @path /app/shopVipUser/updateShopVipLevel
     */
    @RequestMapping(value = "/updateShopVipLevel", method = RequestMethod.POST)
    public ResponseEntity<String> updateShopVipLevel(@RequestHeader(value = "store-id") String storeId,
                                                     @RequestBody JSONObject reqJson) {

        Assert.hasKeyAndValue(reqJson, "levelName", "请求报文中未包含levelName");
        Assert.hasKeyAndValue(reqJson, "vipLevel", "请求报文中未包含vipLevel");
        Assert.hasKeyAndValue(reqJson, "vipLevelId", "vipId不能为空");


        ShopVipLevelPo shopVipLevelPo = BeanConvertUtil.covertBean(reqJson, ShopVipLevelPo.class);
        shopVipLevelPo.setStoreId(storeId);

        return updateShopVipLevelBMOImpl.update(shopVipLevelPo,reqJson);
    }

    /**
     * 微信删除消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /shopVipUser/deleteShopVipLevel
     * @path /app/shopVipUser/deleteShopVipLevel
     */
    @RequestMapping(value = "/deleteShopVipLevel", method = RequestMethod.POST)
    public ResponseEntity<String> deleteShopVipLevel(@RequestHeader(value = "store-id") String storeId,
                                                     @RequestBody JSONObject reqJson) {
        //Assert.hasKeyAndValue(reqJson, "communityId", "小区ID不能为空");

        Assert.hasKeyAndValue(reqJson, "vipLevelId", "vipId不能为空");


        ShopVipLevelPo shopVipLevelPo = BeanConvertUtil.covertBean(reqJson, ShopVipLevelPo.class);
        shopVipLevelPo.setStoreId(storeId);
        return deleteShopVipLevelBMOImpl.delete(shopVipLevelPo);
    }


}
